local Luakongbaibiao = fk.CreateSkill {
  name = "Luakongbaibiao",
  tags = { Skill.Permanent },
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable {
  ["Luakongbaibiao"] = "空白表盘",
  ["General_Skill"]="共鸣技，该技能仅限原武将牌上的角色才能发动。",
  ["#Luakongbaibiao-invoke"] = "空白表盘：请选择其中一个技能获得",
  [":Luakongbaibiao"] = "持恒技，永恒技，<a href='General_Skill'>共鸣技</a>，游戏结束时若你胜利，则你从X个武将中选择一个技能永久获得（X为你已此法获得技能个数，至少为1，至多20）。出牌阶段，你可以调整你的技能。"..
  "<br/><br/><font color='#1E90FF'>【时王一阶】</font>：初始形态，开始收集表盘之旅"..
  "<br/><font color='#FFD700'>【时王二阶】</font>：收集22个技能后变身，获得时空操控的力量"..
  "<br/><font color='#FFA500'>【崇皇时王】</font>：收集全平成主要假面骑士表盘形态后可变身，召唤平成世代骑士之力"..
  "<br/><font color='#9370DB'>【时王逢魔形态】</font>：收集100个技能后变身，至仁至善至高至强的王者！"..
  "<br/><font color='#FF4500'>【逢魔时王】</font>：收集2068个技能后最终变身，成为所有平行宇宙至仁至善至高至强的时间王者！"..
  "<br/><a href='heiseiKamenrider'><font color='#fb2267'>跨越时代的平成假面骑士们，你将承载他们的力量驾驭过去，现在与未来。</font></a>",

  ["heiseiKamenrider"] = "雪山之巅，<font color='#474747'>哭泣的黑色英雄与狂笑的白色恶魔，生死对决</font>;" ..
  "<br/>天亮之后，<font color='#f4f888'>失忆的进化人类与神明的残酷天使，存亡之争</font>;"..
  "<br/>镜面世界，<font color='#6e0000'>蒙面的守护骑士与厮杀的欲望战士，时间循环</font>;"..
  "<br/>魑魅魍魉，<font color='#a548fc'>稳重的音击战士与稚嫩的明日之梦，师徒传承</font>;"..
  "<br/>陨石坠落，<font color='#fc4851'>为爱的神速穿越与轮回的伴你身旁，种族跨越</font>;"..
  "<br/>两族之争，<font color='#941f24'>调停的混血之王与轻浮的最强之人，父子携手</font>;"..
  "<br/>镜头闪烁，<font color='#b71246'>灭世的修卡首领与创世的品红骑士，继往开来</font>;" ..
  "<br/>地球记忆，<font color='#6f04ad'>漆黑的守望之人</font>与<font color='#04ad24'>绿色的恶魔之友</font>，<font color='#6f04ad'>细数</font><font color='#04ad24'>罪孽</font>;" ..
  "<br/>国家三分，<font color='#0730ca'>失忆的天才英雄与非人的肌肉笨蛋</font>，<font color='#ff0000'>世界融合</font>;" ..
  "",
  ["@Luakongbaibiao_collected"] = "持有表盘",
}


Luakongbaibiao:addLoseEffect(function(self, player, is_death)
  local room = player.room
  room:handleAddLoseSkills(player, Luakongbaibiao.name, nil, false, true)
end)


Luakongbaibiao:addAcquireEffect(function(self, player, is_start)
  if not (player.general == "QXzio" or player.general == "QXzio2" or player.general == "QXgrandzio" or player.general == "QXomazio" or player.general == "QXOOOmazio"
  or player.general == "DECADEzio" or player.general == "Kuugazio" or player.general == "Wzio" or player.general == "Buildzio"
  or player.general == "Hibikizio" or player.general == "Kabutozio" or player.general == "Agitozio" or player.general == "Kivazio" or player.general == "Ryukizio") then
    return
  end
  local gamedata = player:getGlobalSaveState("ZIO")
  if gamedata and gamedata.skills then
    if #gamedata.skills >= 22 and player.general == "QXzio" then
      player.general = "QXzio2"
    end
    if #gamedata.skills >= 100 and player.general == "QXzio2" then
      player.general = "QXomazio"
    end
    if #gamedata.skills >= 2068 and player.general == "QXomazio" then
      player.general = "QXOOOmazio"
    end
    print("【时王】玩家 ",player._splayer:getScreenName()," 当前的表盘数为", #gamedata.skills)
    player.room:setPlayerMark(player, "@Luakongbaibiao_collected", #gamedata.skills)
    player.room:handleAddLoseSkills(player, table.concat(gamedata.skills, "|"), nil, false, true)
  end
  player.deputyGeneral = ""
  player.room:broadcastProperty(player, "general")
  player.room:broadcastProperty(player, "deputyGeneral")
end)


Luakongbaibiao:addEffect(fk.GameStart, {
  is_delay_effect = true,
  global = true,
  priority = 1000,
  can_trigger = function(self, event, target, player, data)
    return player.general == "QXzio" or player.general == "QXzio2" or player.general == "QXgrandzio" or player.general == "QXomazio" or player.general == "QXOOOmazio" 
    or player.general == "DECADEzio" or player.general == "Kuugazio" or player.general == "Wzio" or player.general == "Buildzio" or player.general == "Hibikizio"
    or player.general == "Hibikizio" or player.general == "Kabutozio" or player.general == "Agitozio" or player.general == "Kivazio" or player.general == "Ryukizio"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room

    -- 彩蛋：时王来打装甲
    if (player.general == "QXzio" or player.general == "QXzio2" or player.general == "QXomazio" or player.general == "QXOOOmazio" ) then
      local caidan = {"取消"}
      
      if player:hasSkill("kaman_zhu__dianran") then
        table.insert(caidan, "时王空我装甲")
      end
      if player:hasSkill("chaoshenjuexing") then
        table.insert(caidan, "时王亚极陀装甲")
      end
      if player:hasSkill("regrets__jianglin") then
        table.insert(caidan, "时王龙骑装甲")
      end
      if player:hasSkill("huoyangu") then
        table.insert(caidan, "时王响鬼装甲")
      end
      if player:hasSkill("baichongjian") then
        table.insert(caidan, "时王甲斗装甲")
      end
      if player:hasSkill("mohuangjian") then
        table.insert(caidan, "时王月骑装甲")
      end
      if player:hasSkill("kahe") then
        table.insert(caidan, "时王帝骑装甲")
      end
      if player:hasSkill("jifeng") then
        table.insert(caidan, "时王双骑装甲")
      end
      if player:hasSkill("first_2v2_2hu_Third_place__build_pingguan")
      and player:hasSkill("first_2v2_2hu_Third_place__build_dapei")
      and player:hasSkill("first_2v2_2hu_Third_place__build_Hl")then
        table.insert(caidan, "时王创骑装甲")
      end

      if #caidan == 10 then
        table.insert(caidan, "崇皇时王")
      end
      
      
      
      
      local choice = room:askToChoice(player, {
        choices = caidan,
        skill_name = "Luakongbaibiao",
        prompt = "请选择一个表盘装甲进行变身"
      })
      if choice == "时王帝骑装甲" then
        player.general = "DECADEzio"
        room:broadcastProperty(player, "general")
      elseif choice == "时王空我装甲" then
        player.general = "Kuugazio"
        room:broadcastProperty(player, "general")
      elseif choice == "时王双骑装甲" then
        player.general = "Wzio"
        room:broadcastProperty(player, "general")
      elseif choice == "时王创骑装甲" then
        player.general = "Buildzio"
        room:broadcastProperty(player, "general")
      elseif choice == "时王响鬼装甲" then
        player.general = "Hibikizio"
        room:broadcastProperty(player, "general")
      elseif choice == "时王甲斗装甲" then
        player.general = "Kabutozio"
        room:broadcastProperty(player, "general")
      elseif choice == "时王亚极陀装甲" then
        player.general = "Agitozio"
        room:broadcastProperty(player, "general")
      elseif choice == "时王月骑装甲" then
        player.general = "Kivazio"
        room:broadcastProperty(player, "general")
      elseif choice == "时王龙骑装甲" then
        player.general = "Ryukizio"
        room:broadcastProperty(player, "general")
      elseif choice == "崇皇时王" then
        player.general = "QXgrandzio"
        room:broadcastProperty(player, "general")
      end
    end


    if player.general == "QXzio" then
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao1&")
    elseif player.general == "QXgrandzio" then
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao2&")
      room:sendLog{ type = "庆贺钟鸣，时代未熄。", toast = true }
      room:sendLog{ type = "庆贺钟鸣，时代未熄。", toast = true }
      room:sendLog{ type = "庆贺钟鸣，时代未熄。", toast = true }
    elseif player.general == "QXomazio" then
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao3&")
    elseif player.general == "QXzio2" then
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao4&")
    elseif player.general == "QXOOOmazio" then
      room:sendLog{ type = "至仁至善，逢魔君临。", toast = true }
      room:sendLog{ type = "至仁至善，逢魔君临。", toast = true }
      room:sendLog{ type = "至仁至善，逢魔君临。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao5&")
    elseif player.general == "DECADEzio" then
      room:sendLog{ type = "旅途绵延，泛黄相片。", toast = true }
      room:sendLog{ type = "旅途绵延，泛黄相片。", toast = true }
      room:sendLog{ type = "旅途绵延，泛黄相片。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_decade&")
    elseif player.general == "Kuugazio" then
      room:sendLog{ type = "新的英雄，新的传奇。", toast = true }
      room:sendLog{ type = "新的英雄，新的传奇。", toast = true }
      room:sendLog{ type = "新的英雄，新的传奇。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_Kuuga&")
    elseif player.general == "Wzio" then
      room:sendLog{ type = "双位一体，所向披靡。", toast = true }
      room:sendLog{ type = "双位一体，所向披靡。", toast = true }
      room:sendLog{ type = "双位一体，所向披靡。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_W&")
    elseif player.general == "Buildzio" then
      room:sendLog{ type = "胜法既定，绝佳配对。", toast = true }
      room:sendLog{ type = "胜法既定，绝佳配对。", toast = true }
      room:sendLog{ type = "胜法既定，绝佳配对。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_Build&")
    elseif player.general == "Hibikizio" then
      room:sendLog{ type = "千锤百炼，鬼之战士。", toast = true }
      room:sendLog{ type = "千锤百炼，鬼之战士。", toast = true }
      room:sendLog{ type = "千锤百炼，鬼之战士。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_Hibiki&")
    elseif player.general == "Kabutozio" then
      room:sendLog{ type = "行天之道，总司一切。", toast = true }
      room:sendLog{ type = "行天之道，总司一切。", toast = true }
      room:sendLog{ type = "行天之道，总司一切。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_Kabuto&")
    elseif player.general == "Agitozio" then
      room:sendLog{ type = "灵魂闪耀，为人而战。", toast = true }
      room:sendLog{ type = "灵魂闪耀，为人而战。", toast = true }
      room:sendLog{ type = "灵魂闪耀，为人而战。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_Agito&")
    elseif player.general == "Kivazio" then
      room:sendLog{ type = "挣脱锁链，月下独弦。", toast = true }
      room:sendLog{ type = "挣脱锁链，月下独弦。", toast = true }
      room:sendLog{ type = "挣脱锁链，月下独弦。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_Kiva&")
    elseif player.general == "Ryukizio" then
      room:sendLog{ type = "宿命缠身，无战不存。", toast = true }
      room:sendLog{ type = "宿命缠身，无战不存。", toast = true }
      room:sendLog{ type = "宿命缠身，无战不存。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_Ryuki&")
    elseif player.general == "Bladezio" then
      room:sendLog{ type = "王牌奇迹，剑斩命运。", toast = true }
      room:sendLog{ type = "王牌奇迹，剑斩命运。", toast = true }
      room:sendLog{ type = "王牌奇迹，剑斩命运。", toast = true }
      room:broadcastPlaySound("./packages/internetMEME/audio/skill/#Luakongbaibiao_Blade&")
    end
  end,
})



local function chooseplayerskill(player, room, prompt)
  prompt = prompt or "#playerskillpo-choose"
  local skills = {}
  for _, s in ipairs(player.player_skills) do
    if not (s.attached_equip or s.name[#s.name] == "&") and not string.find(s.name, "#") then
      table.insertIfNeed(skills, s.name)
    end
  end

  local result = room:askToCustomDialog(player, {
    skill_name = "选择",
    qml_path = "packages/utility/qml/ChooseSkillBox.qml",
    extra_data = { skills, 0, #skills,  prompt }
  }
  )
  if result ~= "" then
    return result
  end
  return {}
end



Luakongbaibiao:addEffect(fk.GameFinished, {
  anim_type = "control",
  is_delay_effect = true,
  mute = true,
  priority = 1000,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(Luakongbaibiao.name,true, true) then
      return player.general == "QXzio" or player.general == "QXzio2" or player.general == "QXgrandzio" or player.general == "QXomazio" or player.general == "QXOOOmazio"
      or player.general == "DECADEzio" or player.general == "Kuugazio" or player.general == "Wzio" or player.general == "Buildzio" 
      or player.general == "Hibikizio" or player.general == "Kabutozio" or player.general == "Agitozio" or player.general == "Kivazio" or player.general == "Ryukizio"
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local gamedata = player:getGlobalSaveState("ZIO")
    local num = 1
    if gamedata and gamedata.skills then
      num = math.min(math.max(1, #gamedata.skills), 20)
    else
      gamedata.generals = {}
      gamedata.skills = {}
    end
    local black_package_extensionName = {"variation_hegemony","hegemony",}

    local win = data:split("+")
    if table.contains(win, player.role) then
      local adjust_choice = room:askToChoice(player, {
        choices = {"调整存档表盘", "选择新表盘"},
        skill_name = "Luakongbaibiao",
        prompt = "是否调整你的存档表盘？"
      })

      if adjust_choice == "调整存档表盘" then
        local confirm = room:askToChoice(player, {
          choices = {"确认删除", "取消"},
          skill_name = "Luakongbaibiao",
          prompt = "警告：该操作会从存档中永久删除该表盘！请谨慎操作。"
        })
        if confirm ~= "确认删除" then return false end

        local skills = chooseplayerskill(player, room, "请选择要删除的表盘：") or {}
        if table.contains(skills, "Luakongbaibiao") then
          table.removeOne(skills, "Luakongbaibiao")
        end

        if #skills > 0 then
          if gamedata and gamedata.skills then
            local skills_to_delete_from_archive = {}
            local skills_to_delete_from_game = {}
            for _, skill_to_delete in ipairs(skills) do
              if table.contains(gamedata.skills, skill_to_delete) then
                table.insert(skills_to_delete_from_archive, skill_to_delete)
              else
                table.insert(skills_to_delete_from_game, skill_to_delete)
              end
            end
            if #skills_to_delete_from_archive > 0 then
              for _, skill_to_delete in ipairs(skills_to_delete_from_archive) do
                table.removeOne(gamedata.skills, skill_to_delete)
              end
              player:saveGlobalState("ZIO", gamedata)
              print("【时王】玩家 ",player._splayer:getScreenName()," 当前的表盘数为", #gamedata.skills)
              player.room:setPlayerMark(player, "@Luakongbaibiao_collected", #gamedata.skills)
            end
            room:handleAddLoseSkills(player, "-" .. table.concat(skills, "|-"), nil, false, true)
          else
            room:handleAddLoseSkills(player, "-" .. table.concat(skills, "|-"), nil, false, true)
          end
        end
      end

      local generals, skills = {}, {}
      for _, general in pairs(Fk.generals) do
        local gskills = {}
        for _, skillName in ipairs(general:getSkillNameList()) do
          local s = Fk.skills[skillName]
          if not skillName:startsWith("#") and not skillName:endsWith("&") and skillName ~= self.name and not s:isEquipmentSkill() and
          not s.cardSkill and s:isPlayerSkill() and not table.contains(black_package_extensionName, s.package.extensionName) then
            table.insert(gskills, skillName)
          end
        end
        if #gskills > 0 then
          table.insert(generals, general.name)
          table.insertTable(skills, gskills)
        end
      end
      if #generals == 0 then return false end
      generals = table.random(generals, num)
      local canskills = {}
      for _, general in ipairs(generals) do
        local gskills = {}
        for _, skillName in ipairs(Fk.generals[general]:getSkillNameList()) do
          if table.contains(skills, skillName) then
            table.insert(gskills, skillName)
          end
        end
        if #gskills > 0 then
          table.insert(canskills, gskills)
        end
      end
      local choices = U.askToChooseGeneralSkills(player, {
        generals = generals,
        skills = canskills,
        min_num = 1,
        max_num = 1,
        skill_name = Luakongbaibiao.name,
        prompt = "#Luakongbaibiao-invoke",
        cancelable = false,
      })
      room:handleAddLoseSkills(player, table.concat(choices, "|"), nil, false, true)
      table.insert(gamedata.skills, choices[1])
      for _, general in ipairs(generals) do
        if table.find(Fk.generals[general]:getSkillNameList(), function (s)
          return s == choices[1]
        end) then
          table.insert(gamedata.generals, general)
          break
        end
      end
      player:saveGlobalState("ZIO", gamedata)
    end
  end,
})


Luakongbaibiao:addEffect("active", {
  prompt = "klskillopen",
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    return player.id > 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local mode_choices = {"删除表盘（仅本局游戏）", "永久删除存档中的表盘"}
    local mode_result = room:askToChoice(player, {
      choices = mode_choices,
      skill_name = "Luakongbaibiao",
      prompt = "请选择删除模式："
    })
    if not mode_result then return false end
    local is_permanent_delete = (mode_result == "永久删除存档中的表盘")
    if is_permanent_delete then
      local confirm = room:askToChoice(player, {
        choices = {"确认删除", "取消"},
        skill_name = "Luakongbaibiao",
        prompt = "警告：该操作会从存档中永久删除该表盘！请谨慎操作。"
      })
      if confirm ~= "确认删除" then
        return false
      end
    end
    local skills = chooseplayerskill(player, room, "请选择要删除的表盘：") or {}
    if table.contains(skills, "Luakongbaibiao") then
      table.removeOne(skills, "Luakongbaibiao")
    end

    if #skills > 0 then
      if not is_permanent_delete then
        room:handleAddLoseSkills(player, "-" .. table.concat(skills, "|-"), nil, false, true)
      else
        local gamedata = player:getGlobalSaveState("ZIO")
        if gamedata and gamedata.skills then
          local skills_to_delete_from_archive = {}
          local skills_to_delete_from_game = {}
          for _, skill_to_delete in ipairs(skills) do
            if table.contains(gamedata.skills, skill_to_delete) then
              table.insert(skills_to_delete_from_archive, skill_to_delete)
            else
              table.insert(skills_to_delete_from_game, skill_to_delete)
            end
          end
          if #skills_to_delete_from_archive > 0 then
            for _, skill_to_delete in ipairs(skills_to_delete_from_archive) do
              table.removeOne(gamedata.skills, skill_to_delete)
            end
            player:saveGlobalState("ZIO", gamedata)
            print("【时王】玩家 ",player._splayer:getScreenName()," 当前的表盘数为", #gamedata.skills)
            player.room:setPlayerMark(player, "@Luakongbaibiao_collected", #gamedata.skills)
          end
          room:handleAddLoseSkills(player, "-" .. table.concat(skills, "|-"), nil, false, true)
        else
          room:handleAddLoseSkills(player, "-" .. table.concat(skills, "|-"), nil, false, true)
        end
      end
    end
  end,
})



return Luakongbaibiao
